/**
 * 判断点是否在圆形半径内
 * @param {{ x:number, y: number}} point 点
 * @param {{ x:number, y: number}} center 圆心
 * @param {number} radius 圆半径
 * @return {boolean} True or False
 */
export function isInCircle(point, center, radius) {
    const dx = point.x - center.x
    const dy = point.y - center.y
    return dx * dx + dy * dy <= radius * radius
}

/** 获取控制点坐标 */
export function getOCoords(aCoords, controlsVis, controls) {
    function getCoords(type, p) {
        return {
            point: p,
            vis: controlsVis[type],
            radius: controls[type].radius,
            fill: controls[type].fill,
            customDraw: controls[type].customDraw,
        }
    }

    function getPoint(key) {
        switch (key) {
            case 'ml':
                return {x: aCoords.tl.x, y: aCoords.tl.y + (aCoords.bl.y - aCoords.tl.y) / 2}
            case 'mt':
                return {x: aCoords.tl.x + (aCoords.tr.x - aCoords.tl.x) / 2, y: aCoords.tl.y}
            case 'mr':
                return {x: aCoords.tr.x, y: aCoords.tr.y + (aCoords.br.y - aCoords.tr.y) / 2}
            case 'mb':
                return {x: aCoords.bl.x + (aCoords.br.x - aCoords.bl.x) / 2, y: aCoords.bl.y}
            case 'mtr':
                return {x: aCoords.tl.x + (aCoords.tr.x - aCoords.tl.x) / 2, y: aCoords.tl.y - 20}
            case 'delete':
                return {x: aCoords.bl.x + (aCoords.br.x - aCoords.bl.x - 60), y: aCoords.bl.y + 20}
            case 'confirm':
                return {x: aCoords.bl.x + (aCoords.br.x - aCoords.bl.x - 10) , y: aCoords.bl.y + 20}
            default:
                return aCoords[key]
        }
    }

    const result = {}
    Object.keys(controls).forEach((key) => {
        result[key] = getCoords(key, getPoint(key))
    })

    return result
}
